package com.run2code.design.structural.bridge.factory;

import com.run2code.design.structural.bridge.mode.PreAuthMode;
import com.run2code.design.structural.bridge.mode.RealTimeMode;
import com.run2code.design.structural.bridge.pay.AliPay;
import com.run2code.design.structural.bridge.pay.Pay;
import com.run2code.design.structural.bridge.pay.WeChatPay;

/**
 * 工厂模式 - 创建Pay对象
 * 关注公众号【奔跑的码畜】，一起进步不迷路
 *
 * @author 第七人格
 * @date 2023/11/29
 */
public class PayFactory {

    private PayFactory() {

    }

    public static Pay createPay(String type, String mode) {
        if (type.equalsIgnoreCase("alipay")) {
            AliPay aliPay = new AliPay();
            if (mode.equalsIgnoreCase("pre_auth")) {
                aliPay.setPayMode(new PreAuthMode());
            } else if (mode.equalsIgnoreCase("real_time")) {
                aliPay.setPayMode(new RealTimeMode());
            } else {
                throw new IllegalArgumentException("Invalid payment type: " + type);
            }
            return aliPay;
        } else if (type.equalsIgnoreCase("wechat_pay")) {
            WeChatPay weChatPay = new WeChatPay();
            if (mode.equalsIgnoreCase("pre_auth")) {
                weChatPay.setPayMode(new PreAuthMode());
            } else if (mode.equalsIgnoreCase("real_time")) {
                weChatPay.setPayMode(new RealTimeMode());
            } else {
                throw new IllegalArgumentException("Invalid payment type: " + type);
            }
            return weChatPay;
        } else {
            throw new IllegalArgumentException("Invalid payment type: " + type);
        }
    }
}
